<?php

namespace app\admin\controller;

use app\BaseController;
use app\service\article\ArticleCategoryLogic;
use app\service\article\ArticleLogic;
use app\service\category\CategoryLogic;
use think\facade\Session;

class Article extends BaseController
{
    public function index()
    {
        $title = $this->request->param('title');
        if ($this->request->isPost()) {
            $limit = $this->request->param('limit');
            $arrList = ArticleLogic::getInstance()->getList($title, [], $limit);
            return success('ok', $arrList);
        } else {
            $arrTemplate = [
                ['field' => 'id', 'title' => 'id', 'width' => 50],
                ['field' => 'title', 'title' => '标题', 'width' => 200],
                ['field' => 'desc', 'title' => '摘要'],
                ['field' => 'cate_desc', 'title' => '分类', 'width' => 200],
                ['field' => 'view_count', 'title' => '浏览量', 'width' => 80],
                ['field' => 'like_count', 'title' => '点赞数', 'width' => 80],
                ['field' => 'collect_count', 'title' => '收藏数', 'width' => 80],
                ['field' => 'recommend_count', 'title' => '评论数', 'width' => 80],
                ['field' => 'is_open_comment_desc', 'title' => '开启评论', 'width' => 80],
                ['field' => 'is_top_desc', 'title' => '置顶', 'width' => 80],
                ['field' => 'is_recommended_desc', 'title' => '推荐', 'width' => 80],
                ['field' => 'admin_name', 'title' => '添加人', 'width' => 100],
                ['field' => 'create_dt', 'title' => '添加时间', 'width' => 150],
                ['field' => '', 'title' => '操作', 'width' => 140, 'toolbar'=> '#rowBar'],
            ];
            $template = json_encode($arrTemplate);

            return view('', compact('template', 'title'));
        }
    }

    public function addOrEditCom()
    {
        $title = $this->request->param('title');
        $desc = $this->request->param('desc');
        $content = $this->request->param('content');
        $isOpenComment = $this->request->param('is_open_comment', 0);
        $isTop = $this->request->param('is_top', 0);
        $isRecommended = $this->request->param('is_recommended', 0);
        $arrCategoryId = $this->request->param('category_id');
        // 分类
        if (!empty($arrCategoryId)) {
            $arrCategoryId = array_values($arrCategoryId);
        } else {
            $arrCategoryId = [];
        }
        // 图片
//        $img = upload_file($_FILES);
        $img = '';
        $adminId = Session::get('admin_id');
        return [$title, $desc, $content, $arrCategoryId, $img, $isOpenComment, $isTop, $isRecommended, $adminId];
    }

    public function add()
    {
        if ($this->request->isPost()) {
            list($title, $desc, $content, $arrCategoryId, $img, $isOpenComment, $isTop, $isRecommended, $adminId) = $this->addOrEditCom();
            try {
                ArticleLogic::getInstance()->add($title, $desc, $content, $arrCategoryId, $img, $isOpenComment, $isTop, $isRecommended, $adminId);
            } catch (\Exception $e) {
                return error($e->getMessage());
            }
//            return redirect('/admin/article/index');
            return success('已添加');
        } else {
            $arrCategory = CategoryLogic::getInstance()->getAllCategory();
            return view('', ['arrCategory' => $arrCategory]);
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        if ($this->request->isPost()) {
            list($title, $desc, $content, $arrCategoryId, $img, $isOpenComment, $isTop, $isRecommended, $adminId) = $this->addOrEditCom();
            try {
                ArticleLogic::getInstance()->edit($id, $title, $desc, $content, $arrCategoryId, $isOpenComment, $isTop, $isRecommended, $adminId, $img);
            } catch (\Exception $e) {
                return error($e->getMessage());
            }
//            return redirect('/admin/article/index');
            return success('已更新');
        } else {
            $arrCategory = CategoryLogic::getInstance()->getAllCategory();
            $arrData = ArticleLogic::getInstance()->getRowById($id);
            if (empty($arrData)) {
                return error('文章不存在');
            }
            $arrData['category_id'] = ArticleCategoryLogic::getInstance()->getCateByArtId($id);
            return view('', ['arrCategory' => $arrCategory, 'arrData' => $arrData]);
        }
    }

    public function del()
    {
        $id = $this->request->param('id');
        ArticleLogic::getInstance()->del($id);
        return success('已删除');
    }
}